/*
 * 软件版权: 恒生电子股份有限公司
 * 修改记录:
 * 修改日期     修改人员  修改说明
 * ========    =======  ============================================
 * 2021/4/13  lisy31662  新增
 * ========    =======  ============================================
 */
package com.sansi.pencilbook.service.impl;

import com.sansi.pencilbook.DO.Result;
import com.sansi.pencilbook.Enum.RelationEnum;
import com.sansi.pencilbook.entity.RelationBookDTO;
import com.sansi.pencilbook.mapper.RelationBookDTOMapper;
import com.sansi.pencilbook.service.RelationBookServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.List;

/**
 * 功能说明:
 *
 * @author lisy31662
 */
@Service
public class RelationBookServerImpl implements RelationBookServer {

  @Autowired
  private RelationBookDTOMapper relationBookDTOMapper;

  @Override
  public Result getRelationBookDTOAll() {
    try {
      List<RelationBookDTO> list = relationBookDTOMapper.findRelationBookDTOAll();
      return list.size()>0 ? Result.ofSuccess("查询成功", list.size(), list) :
          Result.ofFailed("查询失败");
    } catch (Exception throwables) {
      System.out.println(throwables.getMessage());
      return Result.ofFailed(throwables.getMessage());
    }
  }

  @Override
  public Result getRelationBookDTOByUser(int userId) {
    try {
      List<RelationBookDTO> list = relationBookDTOMapper.findRelationBookDTOByUser(userId);
      return list.size()>0 ? Result.ofSuccess("查询成功", list.size(), list) :
          Result.ofFailed("查询失败");
    } catch (Exception throwables) {
      System.out.println(throwables.getMessage());
      return Result.ofFailed(throwables.getMessage());
    }
  }

  @Override
  public Result getRelationBookDTOByUserRelation(int userId, RelationEnum relation) {
    try {
      List<RelationBookDTO> list = relationBookDTOMapper.findRelationBookDTOByUserRelation(userId, relation.getRelation());
      return list.size()>0 ? Result.ofSuccess("查询成功", list.size(), list) :
          Result.ofFailed("查询失败");
    } catch (Exception throwables) {
      System.out.println(throwables.getMessage());
      return Result.ofFailed(throwables.getMessage());
    }
  }

  @Override
  public Result getRelationBookDTOOne(int userId, int storeId, String isbn) {
    try {
      RelationBookDTO relationBookDTO = relationBookDTOMapper.findRelationBookDTOOne(userId, storeId, isbn);
      return relationBookDTO!=null ? Result.ofSuccess("查询成功", 1, relationBookDTO) :
          Result.ofFailed("查询失败");
    } catch (Exception throwables) {
      System.out.println(throwables.getMessage());
      return Result.ofFailed(throwables.getMessage());
    }
  }
}
